package com.sample.livewallpaperdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.view.MotionEvent;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;

import org.rajawali3d.Object3D;
import org.rajawali3d.animation.Animation3D;
import org.rajawali3d.animation.RotateOnAxisAnimation;
import org.rajawali3d.materials.Material;
import org.rajawali3d.materials.methods.DiffuseMethod;
import org.rajawali3d.materials.textures.ATexture;
import org.rajawali3d.materials.textures.AlphaMapTexture;
import org.rajawali3d.materials.textures.Texture;
import org.rajawali3d.math.vector.Vector3;
import org.rajawali3d.primitives.Plane;
import org.rajawali3d.renderer.Renderer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by jtxiang on 17-7-5.
 */

public class PlaneRenderer extends Renderer {

    private AlphaMapTexture mTimeTexture;
    private Bitmap mTimeBitmap;
    private Canvas mTimeCanvas;
    private Paint mTextPaint;
    private SimpleDateFormat mDateFormat;
    private int mFrameCount;
    private boolean mShouldUpdateTexture;

    public PlaneRenderer(Context context) {
        super(context);
    }

    @Override
    protected void initScene() {
        getCurrentScene().setBackgroundColor(Color.BLUE);

        Material material = new Material();
        try {
            material.addTexture(new Texture("checkerboard", R.drawable.checkerboard));
            material.setColorInfluence(0);
        } catch (ATexture.TextureException e) {
            e.printStackTrace();
        }

        Object3D group = new Object3D();
        Plane plane1 = new Plane(Vector3.Axis.Z);
        plane1.setMaterial(material);
        plane1.setDoubleSided(true);
        plane1.setZ(-0.2);
        group.addChild(plane1);

        Plane plane2 = new Plane(Vector3.Axis.Z);
        plane2.setMaterial(material);
        plane2.setDoubleSided(true);
        plane2.setZ(0);
        group.addChild(plane2);

        // 时间
        Material timeSphereMaterial = new Material();
        timeSphereMaterial.enableLighting(true);
        timeSphereMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
        mTimeBitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
        mTimeTexture = new AlphaMapTexture("timeTexture", mTimeBitmap);
        try {
            timeSphereMaterial.addTexture(mTimeTexture);
        } catch (ATexture.TextureException e) {
            e.printStackTrace();
        }
        timeSphereMaterial.setColorInfluence(1);

        Plane time = new Plane(Vector3.Axis.Z);
        time.setMaterial(timeSphereMaterial);
        time.setColor(Color.RED);
        time.setDoubleSided(true);
        time.setZ(0.2);
        group.addChild(time);

        getCurrentScene().addChild(group);

        getCurrentCamera().enableLookAt();
        getCurrentCamera().setLookAt(0, 0, 0);
        getCurrentCamera().setZ(2);

        // 动画
        Animation3D anim = new RotateOnAxisAnimation(Vector3.Axis.Y, 360);
        anim.setDurationMilliseconds(5000);
        anim.setTransformable3D(group);
        anim.setRepeatCount(1000000);
        anim.setRepeatMode(Animation3D.RepeatMode.REVERSE);
        anim.setInterpolator(new BounceInterpolator());
        getCurrentScene().registerAnimation(anim);
        anim.play();
    }

    public void updateTimeBitmap() {
        new Thread(new Runnable() {
            public void run() {
                if (mTimeCanvas == null) {

                    mTimeCanvas = new Canvas(mTimeBitmap);
                    mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                    mTextPaint.setColor(Color.WHITE);
                    mTextPaint.setTextSize(35);
                    mDateFormat = new SimpleDateFormat("HH:mm:ss",
                            Locale.ENGLISH);
                }
                //
                // -- Clear the canvas, transparent
                //
                mTimeCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
                //
                // -- Draw the time on the canvas
                //
                mTimeCanvas.drawText(mDateFormat.format(new Date()), 75,
                        128, mTextPaint);
                //
                // -- Indicates that the texture should be updated on the OpenGL thread.
                //
                mShouldUpdateTexture = true;
            }
        }).start();
    }

    @Override
    public void onRender(final long elapsedTime, final double deltaTime) {
        //
        // -- not a really accurate way of doing things but you get the point :)
        //
        if (mFrameCount++ >= mFrameRate) {
            mFrameCount = 0;
            updateTimeBitmap();
        }
        //
        // -- update the texture because it is ready
        //
        if (mShouldUpdateTexture) {
            mTimeTexture.setBitmap(mTimeBitmap);
            mTextureManager.replaceTexture(mTimeTexture);
            mShouldUpdateTexture = false;
        }
        super.onRender(elapsedTime, deltaTime);
    }

    public void onTouchEvent(MotionEvent event){
    }

    public void onOffsetsChanged(float x, float y, float z, float w, int i, int j){
    }
}
